package com.yidu.hbats.service.attachment;

import com.yidu.hbats.dao.AtsAttachmentEmpMapper;
import com.yidu.hbats.dao.AtsAttachmentMapper;
import com.yidu.hbats.dao.AtsSchemeAuditMapper;
import com.yidu.hbats.model.AtsAttachment;
import com.yidu.hbats.model.AtsAttachmentEmp;
import com.yidu.hbats.model.AtsSchemeAudit;
import com.yidu.utils.Pages;
import com.yidu.utils.Time;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;


@Service
public class AtsAttachmentServiceImpl implements AtsAttachmentService {
    @Resource
    AtsAttachmentMapper atsAttachmentMapper;
    @Resource
    AtsAttachmentEmpMapper atsAttachmentEmpMapper;
    @Resource
    AtsSchemeAuditMapper atsSchemeAuditMapper;
    @Override
    public int deleteByPrimaryKey(Integer attachId) {
        return atsAttachmentMapper.deleteByPrimaryKey(attachId);
    }

    @Override
    public int insert(AtsAttachment record) {
        return atsAttachmentMapper.insert(record);
    }

    @Override
    public int insertSelective(AtsAttachment record,List<AtsAttachmentEmp> atsAttachmentEmps) {
           int count1 = 0;
           int count2 = 0;
            count1 =  atsAttachmentMapper.insertSelective(record);
        for (AtsAttachmentEmp atsAttachmentEmp : atsAttachmentEmps) {
            atsAttachmentEmp.setAttachId(record.getAttachId());
            count2 = atsAttachmentEmpMapper.insertSelective(atsAttachmentEmp);
        }
        if(count1>0 && count2>0){
            return 1;
        }return 0;
    }

    @Override
    public AtsAttachment selectByPrimaryKey(Integer attachId) {
        return atsAttachmentMapper.selectByPrimaryKey(attachId);
    }

    @Override
    public int updateByPrimaryKeySelective(AtsAttachment record,List<AtsAttachmentEmp> atsAttachmentEmps) {

        int count=0;

             atsAttachmentEmpMapper.deleteBySchemeId(record.getAttachId());
        for (AtsAttachmentEmp atsAttachmentEmp : atsAttachmentEmps) {
            atsAttachmentEmp.setAttachId(record.getAttachId());
             atsAttachmentEmpMapper.insertSelective(atsAttachmentEmp);
        }
            count = atsAttachmentMapper.updateByPrimaryKeySelective(record);


        return count;

    }

    @Override
    public int updateByAtsAttachment(AtsAttachment record) {
        return atsAttachmentMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public int updateByPrimaryKey(AtsAttachment record) {
        return atsAttachmentMapper.updateByPrimaryKey(record);
    }



    @Override
    public List<AtsAttachment> findAll(AtsAttachment record) {
        return atsAttachmentMapper.findAll(record);
    }

    @Override
    public int updateAuditState(AtsAttachment record, AtsSchemeAudit atsSchemeAudit) {
       int count1 =  atsAttachmentMapper.updateByPrimaryKeySelective(record);
       int count2 = atsSchemeAuditMapper.insertSelective(atsSchemeAudit);
       if(count1>0&count2>0){
            return 1;
       }
        return 0;
    }


    @Override
    public List<AtsAttachment> selectAll(Time time, Pages pages, AtsAttachment attachment) {
        return atsAttachmentMapper.selectAll(time,pages,attachment);
    }

    @Override
    public int selectCount(AtsAttachment atsAttachment, Time time) {
        return atsAttachmentMapper.selectCount(atsAttachment,time);
    }

    @Override
    public List<AtsAttachment> selectCheckAll(Time time, Pages pages, AtsAttachment atsAttachment) {
        return atsAttachmentMapper.selectCheckAll(time,pages,atsAttachment);
    }

    @Override
    public int selectCheckCount(AtsAttachment atsAttachment, Time time) {
        return atsAttachmentMapper.selectCheckCount(atsAttachment,time);
    }

    @Override
    public List<AtsAttachment> selectLookAll( String userId,Time time, Pages page, AtsAttachment atsAttachment) {
        return atsAttachmentMapper.selectLookAll(userId,time,page,atsAttachment);
    }

    @Override
    public int selectLookCount( String userId,AtsAttachment atsAttachment, Time time) {
        return atsAttachmentMapper.selectLookCount(userId,atsAttachment,time);
    }


}
